<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="../js/cities.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
<script>
    function outer() {
        //外函数不能使用内函数中的变量
        let name = "outer";
        let obj = {
            name: "haha",
            age: 18
        };
        function inner() {
            //内函数能使用外函数中的变量，那闭包就是这个意思
            console.log(name);
            console.log(obj.name);
        }
        return inner;
    }
    let inner = outer();

    function t1() {
        console.log("t1");
        // t1();
    }

    t1();

    //在city中搜索衡阳县
    position.forEach(sheng => {
        sheng.children.forEach(shi => {
            shi.children.forEach(xiang => {
                if (xiang.value == "衡阳县") {
                    console.log(xiang);
                }
            })
        });
    });

    function searchDeep(array, target) {
        array.forEach(item => {
            if (item.value == target) {
                console.log(item);
                return;
            }

            if (item.children) {
                searchDeep(item.children, target);
                //在非严格模式才支持
                // arguments.callee.call(this, item.children, target);
            }
        });
    }
    searchDeep(position, "衡阳县");
    searchDeep(position, "湛江市");

    function Animal() {
        this.name = "小狗";
        this.age = 2;
        this.say = function () {
            console.log("汪汪汪");
        }
    }

    let obj = {
        name: "haha",
        age: 18,
        hubby: ["java", "c++"],
        cw: new Animal(),
        tdate: Date("2021-06-27")
    };
    //浅拷贝
    let objCopy = Object.assign({}, obj);
    console.log(objCopy === obj);
    console.log(objCopy);
    console.log(obj);
    objCopy.hubby.push("html");
    objCopy.cw.name = "小猫";
    console.log(objCopy);
    console.log(obj);

    function copyDeep(target, source) {
        source = Object.assign(source, target);//将所有数据复制到source中
        //对所有引用数据类型做深层次处理
        for (let key in target) {
            console.log(key);
            let value = target[key];
            if (Array.isArray(value)) {
                source[key] = Array.from(target[key]);
            } else if (value instanceof Date) {
                source[key] = Object.assign({}, value);
            } else if (value instanceof Object) {
                source[key] = Object.assign({}, value);
                copyDeep(source[key], target[key]);
            }
        }
    }

    let obj2 = {};
    copyDeep(obj, obj2);
    console.log("----------------obj2---------------");
    console.log(obj2);
    obj2.hubby.push("Mysql");
    obj2.cw.name = "猴子";
    obj2.tdate = Date.now();
    console.log("----------------obj---------------");
    console.log(obj);
    console.log("----------------obj2---------------");
    console.log(obj2);
    let obj3 = _.cloneDeep(obj);
    console.log("----------------obj3---------------");
    obj3.cw.name = "大狼狗";
    console.log(obj3);
    console.log(obj);

</script>
</body>
</html>